package com.cy.pj.notice.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.notice.dao.SysNoticeDao;
import com.cy.pj.notice.pojo.SysNotice;
import com.cy.pj.notice.service.SysNoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysNoticeServiceImpl implements SysNoticeService {
    //初始化日志对象(org.slf4j.*)
    private static final Logger log =
            LoggerFactory.getLogger(SysNoticeServiceImpl.class);
    @Autowired
    private SysNoticeDao sysNoticeDao;

    @Override
    public List<SysNotice> findNotices(SysNotice sysNotice) {
        long t1 = System.currentTimeMillis();
        log.info("method start {}", t1);//这里的{}表示占位符
        List<SysNotice> list = sysNoticeDao.selectNotices(sysNotice);
        long t2 = System.currentTimeMillis();
        log.info("method end {}", t2);
        return list;
    }

    @Override
    public int saveNotices(SysNotice sysNotice) {
        //...
        int rows = sysNoticeDao.insertNotices(sysNotice);
        return rows;
    }

    @Override
    public SysNotice findById(Long id) {
        SysNotice notice = sysNoticeDao.selectById(id);
        if (notice == null)
            throw new ServiceException("可能记录已经不存在");
        return notice;
    }

    @Override
    public int updateNotice(SysNotice sysNotice) {
        return sysNoticeDao.updateNotice(sysNotice);
    }

    @Override
    public int deleteById(Long... id) {
        int rows = sysNoticeDao.deleteNotices(id);
        if (rows == 0) {
            throw new ServiceException("记录可能已经不存在");
        }
        return rows;
    }
//    public SysNoticeServiceImpl(SysNoticeDao sysNoticeDao){
//        this.sysNoticeDao=sysNoticeDao;
//    }

}
